package com.example.his.api.mis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.PermissionDao;
import com.example.his.api.db.pojo.PermissionEntiy;
import com.example.his.api.mis.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service("MisPermissionService")
public class PermissionServiceImpl implements PermissionService {
  @Resource
  private PermissionDao permissionDao;
  
  @Override
  @Transactional
  public int insert(Map param) {
    PermissionEntiy bean = BeanUtil.toBean(param, PermissionEntiy.class);
    permissionDao.insert(bean);
    Integer id = bean.getId();
    return id;
  }
  
  @Override
  @Transactional
  public int deleteByIds(Integer[] ids) {
    int i = permissionDao.deleteByIds(ids);
    return i;
  }
  
  
  @Override
  @Transactional
  public boolean update(Map param) {
    int rows = permissionDao.update(param);
    return rows == 1;
  }
  
  @Override
  public HashMap searchById(Integer id) {
    HashMap map = permissionDao.searchById(id);
    return map;
  }
  
  @Override
  public PageUtils searchByPage(Map param) {
    ArrayList list = new ArrayList();
    long count = permissionDao.searchCountByPage(param);
    if(count > 0) {
      list = permissionDao.searchByPage(param);
    }
    Integer page = MapUtil.getInt(param, "page");
    Integer length = MapUtil.getInt(param, "length");
    PageUtils pageUtils = new PageUtils(list, count, page, length);
    
    return pageUtils;
  }
  
  @Override
  public ArrayList <HashMap> searchAll() {
    ArrayList <HashMap> list = permissionDao.searchAll();
    return list;
  }
}
